var express = require('express');
var router = express.Router();
var request = require('request');
var qs = require('qs');
var path = require('path');

router.get('/withdraw',function(req,res) { //取款主页
    if(req.session.token) {
        res.sendFile(path.join(__dirname,"../src/templates/Withdraw.html"));
    }else {
        res.redirect("/");
    }
});
router.post('/withdraw',function(req,res) {//取款金额
    if(req.session.token) {
        var url = "http://39.106.9.239/ATM/business/withdraw";
        var data = {
            "amount" : req.body.money
        }
        request({
            url: url,
            method: "post",
            headers: {
                "content-type" : "application/x-www-form-urlencoded",
                "authorization" : req.session.token,
                "deviceNo" : 100
            },
            body: qs.stringify(data)
        },function(_err,_res,_resBody) {
            if(!_err && _res.statusCode == 200) {
                var code = JSON.parse(_resBody).code;
                if(code == 0) {
                    req.session.adviceId = JSON.parse(_resBody).data.adviceId;
                }
                res.send(_resBody);
            }else {
                console.log(_err);
            }
        });
    }else {
        res.redirect("/");
    }
});
router.get('/successfulWithdraw',function(req,res) { //取款成功
    if(req.session.token) {
        res.sendFile(path.join(__dirname,"../src/templates/successfulWithdraw.html"))
    }else {
        res.redirect("/");
    }
});
module.exports = router;